import { browserHistory as history } from 'react-router';
let u = navigator.userAgent;


export const redirect = (url) => {
    history.push(url);
};

export const replace = (url) => {
    history.replace(url);
};

export const backward = ()=>{
    history.goBack();
};


export const numberFormat = (num) => {
  if(typeof num !== "number" || isNaN(num))  {
     return num;
  }
  return num.toFixed(2);
};

export const  getLocalTime = (time,fmt) =>  {
    if (time == null || !time || time == 0) {
        return;
    }
    var fmt = fmt ? fmt : 'yyyy-MM-dd hh:mm:ss';
    var time = new Date(time*1000);
    var z = {
        M: time.getMonth() + 1,
        d: time.getDate(),
        h: time.getHours(),
        m: time.getMinutes(),
        s: time.getSeconds()
    };
    fmt = fmt.replace(/(M+|d+|h+|m+|s+)/g, function(v) {
        return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-2);
    });
    return fmt.replace(/(y+)/g, function(v) {
        return time.getFullYear().toString().slice(-v.length);
    });
}

export const handlerPic = (img,height) => {
    let url = "http://img.rcglife.com/" + img;
    if(height){
        url = url + "-r"+height ;
    }
    return url;
};

export const inRoute = () =>{
    let pathname = window.location.pathname;
    let route = [
        '/login'
    ];
    if(route.indexOf(pathname) != '-1'){
        return true;
    }else{
        return false;
    }
}


export const getCookie = (name) => {

    var arr=document.cookie.split('; '); //多个cookie值是以; 分隔的，用split把cookie分割开并赋值给数组
    for(var i=0;i<arr[i].length;i++) //历遍数组
    {
        var arr2=arr[i].split('='); //原来割好的数组是：user=simon，再用split('=')分割成：user simon 这样可以通过arr2[0] arr2[1]来分别获取user和simon
        if(arr2[0]==name) //如果数组的属性名等于传进来的name
        {
            return arr2[1]; //就返回属性名对应的值
        }
        return ''; //没找到就返回空
    }
}


